package fh.app4stud.mensa;

import android.app.Activity;
import android.content.*;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import fh.app4stud.mensa.data.RestaurantLocation;

import java.util.ArrayList;
import java.util.Iterator;

public class Start extends Activity {

    //constants
    static final private int HOME = Menu.FIRST;
    static final private int NEXT_DAY = Menu.FIRST + 1;
    static final private int SETTINGS = Menu.FIRST + 2;
    static final private int MORE = Menu.FIRST + 3;

    //variables
    private boolean mensaLocationBound = false;
    private boolean isCreated = false;
    private MensaLocationService mensaLocationService;
    private SharedPreferences preference;
    //TODO maybe I can use it for the list
    private Button todayMenu;
    private Button getLocations;
    private Button settings;
    private ArrayList<RestaurantLocation> locationsList = new ArrayList<RestaurantLocation>() ;
    private ListView preferredLocationsView;
    private ArrayList<String> preferredLocationsNames;
    private ArrayAdapter preferredLocationsAA;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //Shared preferences
        Context context = getApplicationContext();
        preference = PreferenceManager.getDefaultSharedPreferences(context);

        getLocations = (Button) findViewById(R.id.get_locations);
        settings = (Button) findViewById(R.id.settings);
        hookUpButtons();





    }

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, MensaLocationService.class);
        startService(intent);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Unbind from the service
        if (mensaLocationBound) {
            unbindService(mConnection);
            mensaLocationBound = false;
        }
    }

    /**
     * @Override public boolean onCreateOptionsMenu(Menu menu) {
     * super.onCreateOptionsMenu(menu);
     * <p/>
     * //Create and add new menu items
     * MenuItem itemHome = menu.add(0, HOME, Menu.NONE, R.string.options_menu_home);
     * MenuItem itemNextDay = menu.add(0, NEXT_DAY, Menu.NONE, R.string.options_menu_next_day);
     * MenuItem itemSettings = menu.add(0, SETTINGS, Menu.NONE, R.string.options_menu_settings);
     * MenuItem itemMore = menu.add(0, MORE, Menu.NONE, R.string.options_menu_more);
     * <p/>
     * // TODO Assign icons
     * //itemAdd.setIcon(R.drawable.add_new_item);
     * //itemRem.setIcon(R.drawable.remove_item);
     * <p/>
     * // Allocate shortcuts to each of them.
     * itemHome.setShortcut('0', 'a');
     * itemNextDay.setShortcut('1', 'r');
     * return true;
     * <p/>
     * <p/>
     * //TODO 134 stranica overriding izabrane elemente u meniu
     * }
     */

    public void hookUpButtons() {
        getLocations.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //call the activity to get the data from the web service
                //Intent getData = new Intent(Start.this, LocationList.class);
                //startActivity(getData);
                if(mensaLocationBound){
                    Log.i("button click", "there is a connnection to service");
                   ArrayList<RestaurantLocation> some = mensaLocationService.getRestaurantLocations('c');
                    boolean som = some.isEmpty();
                }
            }
        });
        /*
        todayMenu.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent getMenu = new Intent(Start.this, LocationMenuList.class);
                startActivity(getMenu);
            }
        });
        */
        settings.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //call the activity to get the data from the web service
                Intent preferences = new Intent(Start.this, LocationListPreferences.class);
                startActivity(preferences);
            }
        });
    }

    /**
     * @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
     * super.onActivityResult(requestCode, resultCode, data);
     * <p/>
     * switch (requestCode) {
     * //case ()  TODO different cases of requests.....
     * //  if (resultCode == Activity.RESULT_OK) {
     * }
     * <p/>
     * //go to List Activity
     * Intent goToTodayMenu = new Intent(Start.this, LocationMenuList.class);
     * startActivity(goToTodayMenu);
     * }
     */

    private ServiceConnection mConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

            //we've bound to local service, cast the Ibinder and get localSErvice instance
            MensaLocationService.LocalBinder binder = (MensaLocationService.LocalBinder) iBinder;
            mensaLocationService = binder.getService();
            mensaLocationBound = true;
        }

        public void onServiceDisconnected(ComponentName componentName) {
            mensaLocationBound = false;
        }
    };

}